package gervosou;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.DropMode;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JSeparator;
import javax.swing.JList;
import javax.swing.JScrollBar;
import javax.swing.JRadioButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import java.awt.Container;

public class VueNouvMandat extends Vue {

	private Societe _societe;
	
	private JPanel panel;
	
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	private JTextField display;
	
	private JButton btnCreer;
	private JButton btnAnnuler1;
	private JButton btnSelectionner;
	private JButton btnAnnuler2;
	private JButton btnAffecter;
	private JButton btnAnnuler3;
	
	private JLabel lblNom;
	private JLabel lblPrenom;
	private JLabel lblSociete;
	private JLabel lblComptes;
	private JLabel lblDroits;
	
	private JSeparator separator2;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();

	private JRadioButton rdbtnA;
	private JRadioButton rdbtnB;
	private JRadioButton rdbtnC;
	
	private DefaultComboBoxModel comboModel;
	private JComboBox comboBoxSoc;
	private JComboBox comboBoxCompte;
	
	private DefaultListModel model;
	
	private Container co;
	
	public VueNouvMandat(Controleur c) {
		super(c);
		setTitle("Ajout Mandataires");
		setBounds(100, 100, 500, 500);
	
		panel = new JPanel();
		panel.setBorder(new EmptyBorder(0, 0, 0, 0));
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		lblNom = new JLabel("Nom :");
		lblNom.setBounds(150, 34, 44, 15);
		panel.add(lblNom);
		
		textFieldNom = new JTextField();
		textFieldNom.setBounds(234, 32, 114, 19);
		panel.add(textFieldNom);
		textFieldNom.setColumns(10);
		
		lblPrenom = new JLabel("Prenom :");
		lblPrenom.setBounds(132, 63, 61, 15);
		panel.add(lblPrenom);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setBounds(234, 63, 114, 19);
		panel.add(textFieldPrenom);
		textFieldPrenom.setColumns(10);
		
		lblSociete = new JLabel("Société :");
		lblSociete.setBounds(133, 108, 61, 15);
		panel.add(lblSociete);
		
		comboBoxSoc = new JComboBox();
		comboBoxSoc.setBounds(231, 103, 140, 25);
		panel.add(comboBoxSoc);
	
		btnSelectionner = new JButton("Selectionner");
		btnSelectionner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//	String nom = comboBoxSoc.getName();
				// liaison de la vue avec l'objet observe
				setSociete (getControleur().rechMandat(comboBoxSoc.getSelectedItem().toString()));
			}
		});
		btnSelectionner.setBounds(77, 160, 117, 25);
		panel.add(btnSelectionner);
		
		btnAnnuler2 = new JButton("Annuler");
		btnAnnuler2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueNouvMandat.this);
			}
		});
		btnAnnuler2.setBounds(265, 160, 107, 25);
		panel.add(btnAnnuler2);
		
		separator2 = new JSeparator();
		separator2.setBounds(77, 213, 295, 2);
		panel.add(separator2);
		
		lblComptes = new JLabel("Comptes :");
		lblComptes.setBounds(123, 227, 71, 15);
		panel.add(lblComptes);
		
		lblDroits = new JLabel("Droits(niveau) :");
		lblDroits.setBounds(87, 351, 107, 15);
		panel.add(lblDroits);
		
		rdbtnA = new JRadioButton("A");
		buttonGroup.add(rdbtnA);
		rdbtnA.setBounds(77, 374, 44, 23);
		panel.add(rdbtnA);
		
		rdbtnB = new JRadioButton("B");
		buttonGroup.add(rdbtnB);
		rdbtnB.setBounds(178, 374, 44, 23);
		panel.add(rdbtnB);
		
		rdbtnC = new JRadioButton("C");
		buttonGroup.add(rdbtnC);
		rdbtnC.setBounds(277, 374, 44, 23);
		panel.add(rdbtnC);
		
		btnAffecter = new JButton("Affecter");
		btnAffecter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nom = textFieldNom.getText();
				String prenom = textFieldPrenom.getText();
				//String societe = comboBoxSoc.getName();
				String type; 
				if (rdbtnA.isSelected()) {
					type = "A";}
				else if (rdbtnB.isSelected()) {
					type = "B";}
				else {
					type = "C";}

				getControleur().nouvMandataire(nom, prenom, comboBoxSoc.getSelectedItem().toString(), type);
			}
		});
		btnAffecter.setBounds(87, 417, 107, 25);
		panel.add(btnAffecter);
		
		btnAnnuler3 = new JButton("Annuler");
		btnAnnuler3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueNouvMandat.this);
			}
		});
		btnAnnuler3.setBounds(265, 417, 107, 25);
		panel.add(btnAnnuler3);
		
		comboBoxCompte = new JComboBox();
		comboBoxCompte.setBounds(234, 222, 137, 24);
		panel.add(comboBoxCompte);
	}
	
	private void setSociete(Societe soc) {
		_societe = soc;
	}
	
	public void alimenteS() {
		
		 HashMap<String, Societe> soc;
		 soc = getControleur().getSocietes();
		 for (String mapKey : soc.keySet()) {
		 comboBoxSoc.addItem(soc.get(mapKey).getNom());
		 }
	}
	
	 public void alimenteC(Societe soc) {
		HashMap<String, Societe> s;
		 //CompteSociete cs;
		 s = getControleur().getSocietes();
		// cs.getNumCompte();
		 
		 for (CompteSociete cs1 : soc.getComptes()) {			 
		for (String mapKey : s.keySet()) {
			 
		 }
				comboBoxCompte.addItem(cs1.getNumCompte());			}
	 }
		 
		 
		 
		 
//		 HashSet<String, CompteSociete> cs;
//		 cs = soc.getComptes();
//		 CompteSociete cs;
//		 cs : soc.getComptes();
//		 Vector<String> vec = new Vector<String>();
//
//		 for (String mapKey : cs.keySet()) {
//			 String num = toString().valueOf((cs.get(mapKey).getNumCompte()));
//			 vec.add(num);
//		 }
//		 for(int i = vec.size()-1 ; i >= 0 ; i--)
//		 {
//			 model.addElement(vec.elementAt(i));
//		 }
//			 
//	 }
}
	

	
	
//	public void alimenteC(Societe soc) {
//	list.removeAll();
//	
//	for (CompteSociete cs : soc.getComptes()) {
//	list.("numero "
//	+ String.valueOf(cs.getNumCompte()) + " solde "
//	+ String.valueOf(cs.getSolde()) + "\n");
//	}
//	}
//}